package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueCollectionHomeMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueCollectionHomeService;
import com.zjdiepu.www.model.dh.dto.DhOverdueCollectionHome;
import com.zjdiepu.www.model.dh.ext.DhOverdueCollectionHomeExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月14日 14时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueCollectionHomeServiceImpl extends BaseService implements IDhOverdueCollectionHomeService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueCollectionHomeServiceImpl.class);

	@Autowired
	private DhOverdueCollectionHomeMapper dhOverdueCollectionHomeMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueCollectionHome dhOverdueCollectionHome, OperateType operType) {
		switch (operType) {
		case add:
			dhOverdueCollectionHome.initInsertData(dhOverdueCollectionHome.getModifier(), dhOverdueCollectionHome.getModifierName());
			dhOverdueCollectionHomeMapper.insert(dhOverdueCollectionHome);
			break;
		case edit:
			dhOverdueCollectionHome.initUpdateData(dhOverdueCollectionHome.getModifier(), dhOverdueCollectionHome.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueCollectionHomeMapper.updateByPrimaryKeySelective(dhOverdueCollectionHome);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueCollectionHome.initDeleteData(dhOverdueCollectionHome.getModifier(), dhOverdueCollectionHome.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueCollectionHomeMapper.updateByPrimaryKeySelective(dhOverdueCollectionHome);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueCollectionHome> selectByExt(DhOverdueCollectionHomeExt dhOverdueCollectionHomeExt) {
		return dhOverdueCollectionHomeMapper.selectByExt(dhOverdueCollectionHomeExt);
	}
	
	@Override
	public int countByExt(DhOverdueCollectionHomeExt dhOverdueCollectionHomeExt){
		return dhOverdueCollectionHomeMapper.countByExt(dhOverdueCollectionHomeExt);
	}
	
	@Override
	public DhOverdueCollectionHome selectById(String id) {
		return dhOverdueCollectionHomeMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueCollectionHomeMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueCollectionHome dhOverdueCollectionHome) {
		return dhOverdueCollectionHomeMapper.countRefined(dhOverdueCollectionHome);
	}

	@Override
	public DhOverdueCollectionHome selectRefined(DhOverdueCollectionHome dhOverdueCollectionHome) {
		return dhOverdueCollectionHomeMapper.selectRefined(dhOverdueCollectionHome);
	}

	@Override
	public boolean repeat(DhOverdueCollectionHome dhOverdueCollectionHome) {
		int count = countRefined(dhOverdueCollectionHome);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueCollectionHome.getId())) {
			return true;
		}
		DhOverdueCollectionHome dbItem = selectRefined(dhOverdueCollectionHome);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueCollectionHome.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueCollectionHomeMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
